//
//  HomeOnlineTrainListTableViewCell.m
//  ZJControlsClass
//
//  Created by wandoufo on 2024/12/8.
//

#import "HomeOnlineTrainListTableViewCell.h"
#import "HomeOnlineTrainModel.h"
#import "HomeOnlineTrainDetailViewController.h"
#import "ZJCreateOnlineCourseWorking.h"


@interface HomeOnlineTrainListTableViewCell ()

@property (nonatomic, strong) UILabel *courseNameTitleLabel;    // 课程名称标题
@property (nonatomic, strong) UILabel *courseNameLabel;         // 课程名称内容
@property (nonatomic, strong) UILabel *lessonCountTitleLabel;   // 课程节数标题
@property (nonatomic, strong) UILabel *lessonCountLabel;        // 课程节数内容
@property (nonatomic, strong) UILabel *progressTitleLabel;      // 进度标题
@property (nonatomic, strong) UILabel *progressLabel;           // 进度内容
@property (nonatomic, strong) UIButton *actionButton;           // 操作按钮
@property (nonatomic, strong) UIView *separatorLine;            // 分割线
@property (nonatomic, strong) UIView *containerView;            // 背景容器视图

@end



@implementation HomeOnlineTrainListTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        [self setupUI];
        
    }
    return self;
}

- (void)setupUI {
    // 添加背景容器视图
    self.containerView = [[UIView alloc] init];
    self.containerView.backgroundColor = [UIColor whiteColor];
    self.containerView.layer.cornerRadius = 8;
    self.contentView.layer.masksToBounds = true;
    [self.contentView addSubview:self.containerView];
    
    // 设置背景视图约束
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(10);
        make.right.equalTo(self.contentView).offset(-10);
        make.top.equalTo(self.contentView).offset(12);
        make.bottom.equalTo(self.contentView);
    }];
    
    // 课程名称
    self.courseNameTitleLabel = [self createLabelWithFont:14 textColor:[UIColor colorWithHexString:@"#666666"]];
    self.courseNameTitleLabel.text = @"课程名称：";
    self.courseNameLabel = [self createLabelWithFont:14 textColor:[UIColor colorWithHexString:@"#181818"]];
    
    // 课程节数
    self.lessonCountTitleLabel = [self createLabelWithFont:14 textColor:[UIColor colorWithHexString:@"#666666"]];
    self.lessonCountTitleLabel.text = @"课程节数：";
    self.lessonCountLabel = [self createLabelWithFont:14 textColor:[UIColor colorWithHexString:@"#181818"]];
    
    // 进度
    self.progressTitleLabel = [self createLabelWithFont:14 textColor:[UIColor colorWithHexString:@"#666666"]];
    self.progressTitleLabel.text = @"已完成进度：";
    self.progressLabel = [self createLabelWithFont:14 textColor:[UIColor colorWithHexString:@"#181818"]];
    
    // 按钮
    self.actionButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.actionButton setTitle:@"" forState:UIControlStateNormal];
    [self.actionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.actionButton.backgroundColor = [UIColor mainColor];
    [self.actionButton.titleLabel setFont: [UIFont systemFontOfSize:14 weight:UIFontWeightMedium]];
    self.actionButton.layer.cornerRadius = 4;
    [self.actionButton addTarget:self action:@selector(actionButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    // 分割线
    self.separatorLine = [[UIView alloc] init];
    self.separatorLine.backgroundColor = [UIColor colorWithHexString:@"#E4E4E4"];
    
    // 修改其他组件的父视图为containerView（原来是添加到contentView）
    [self.containerView addSubview:self.courseNameTitleLabel];
    [self.containerView addSubview:self.courseNameLabel];
    [self.containerView addSubview:self.lessonCountTitleLabel];
    [self.containerView addSubview:self.lessonCountLabel];
    [self.containerView addSubview:self.progressTitleLabel];
    [self.containerView addSubview:self.progressLabel];
    [self.containerView addSubview:self.actionButton];
    [self.containerView addSubview:self.separatorLine];
    
    // 修改所有约束中的参照视图为containerView（原来是contentView）
    [self.courseNameTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.containerView).offset(10);
        make.top.equalTo(self.containerView).offset(12);
    }];
    
    [self.courseNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.courseNameTitleLabel.mas_right).offset(5);
        make.centerY.equalTo(self.courseNameTitleLabel);
        make.right.lessThanOrEqualTo(self.containerView).offset(-12);
    }];
    
    [self.lessonCountTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.courseNameTitleLabel);
        make.top.equalTo(self.courseNameTitleLabel.mas_bottom).offset(10);
    }];
    
    [self.lessonCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.lessonCountTitleLabel.mas_right).offset(5);
        make.centerY.equalTo(self.lessonCountTitleLabel);
        make.right.lessThanOrEqualTo(self.containerView).offset(-12);
    }];
    
    [self.progressTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.courseNameTitleLabel);
        make.top.equalTo(self.lessonCountTitleLabel.mas_bottom).offset(10);
    }];
    
    [self.progressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.progressTitleLabel.mas_right).offset(5);
        make.centerY.equalTo(self.progressTitleLabel);
        make.right.lessThanOrEqualTo(self.containerView).offset(-12);
    }];
    
   
    
    [self.separatorLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.containerView).offset(12);
        make.right.equalTo(self.containerView).offset(-12);
        make.bottom.equalTo(self.containerView).offset(-52);
        make.height.equalTo(@0.5);
        make.top.equalTo(self.progressTitleLabel.mas_bottom).offset(12);
    }];
    
    
    [self.actionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.containerView).offset(-12);
//        make.centerY.equalTo(self.containerView);
        make.top.equalTo(self.separatorLine).offset(12);
        make.width.equalTo(@79);
        make.height.equalTo(@28);
//        make.bottom.equalTo(self.containerView).offset(-12);
    }];
    
}

- (UILabel *)createLabelWithFont:(CGFloat)fontSize textColor:(UIColor *)color {
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:fontSize];
    label.textColor = color;
    return label;
}

- (void)setTrainModel:(HomeOnlineTrainModel *)trainModel {
    _trainModel = trainModel;
}

- (void)setModel:(HomeOnlineCourseModel *)model {
    _model = model;
//    model.completedCoursewareNum = 1;
    self.courseNameLabel.text = model.name;
    self.lessonCountLabel.text = [NSString stringWithFormat:@"%ld节", model.totalCoursewareNum];
    self.progressLabel.text = [NSString stringWithFormat:@"%.0f%%", (model.completedCoursewareNum * 100.0 / model.totalCoursewareNum)];
    if (_model.status == 1 || _model.status == 0) {
        [self.separatorLine mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.containerView).offset(-52);
        }];
        self.actionButton.hidden = false;
        self.separatorLine.hidden = false;
        if (_model.status == 0) {
            [self.actionButton setTitle:@"开始学习" forState:UIControlStateNormal];
        } else {
            [self.actionButton setTitle:@"继续培训" forState:UIControlStateNormal];
        }
        
    } else {
        self.actionButton.hidden = true;
        self.separatorLine.hidden = true;
        [self.separatorLine mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.containerView).offset(0);
        }];
    }
    
}

- (void)actionButtonClicked:(UIButton *)sender {
    if (self.model.status == 0) {
        
//        coursewareId    课件ID    query    true
//        string
//        taskId    培训任务ID    query    true
//        string
//        courseId
        [[ZJCreateOnlineCourseWorking new] dataTaskWithParam:@{@"taskId":_trainModel.identifier,@"courseId": _model.identifier
                                                               ,@"coursewareId" : _model.coursewares.count > 0 ? _model.coursewares.firstObject.identifier : @"0",
                                                               @"total": @"0"
                                                             } completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
            
        }];
        // 创建学习记录,
     
    }
        [self pushToDetailVC];
    
}

- (void)pushToDetailVC {
    HomeOnlineTrainDetailViewController *detailVC = [[HomeOnlineTrainDetailViewController alloc] init];
    detailVC.model = self.model;
    detailVC.taskId = self.trainModel.identifier;
//    UIViewController */*currentVC*/ = [UIViewController currentViewController];
    [[SaViewControllerUtils currentViewController].navigationController pushViewController:detailVC animated:YES];
}




@end
